package com.sangfor.ssl.common;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import com.sangfor.ssl.rdp.utils.crypto.Base64;
import com.sangfor.ssl.rdp.utils.crypto.Crypto;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import okhttp3.internal.ws.WebSocketProtocol;

/* loaded from: classes2.dex */
public class VpnCommon {
    private static final String EASYAPP_META_MARK = "com.sangfor.easyapp.version";
    private static final int HIDMD5_LENGTH = 32;
    private static final String TAG = "VpnCommon";

    public static long ipToLong(String str) {
        long[] jArr = new long[4];
        int indexOf = str.indexOf(".");
        int i = indexOf + 1;
        int indexOf2 = str.indexOf(".", i);
        int i2 = indexOf2 + 1;
        int indexOf3 = str.indexOf(".", i2);
        if (indexOf != 0 && indexOf2 > indexOf && indexOf3 > indexOf2) {
            try {
                jArr[0] = Long.parseLong(str.substring(0, indexOf));
                jArr[1] = Long.parseLong(str.substring(i, indexOf2));
                jArr[2] = Long.parseLong(str.substring(i2, indexOf3));
                jArr[3] = Long.parseLong(str.substring(indexOf3 + 1));
                return (jArr[0] << 24) + (jArr[1] << 16) + (jArr[2] << 8) + jArr[3];
            } catch (NumberFormatException unused) {
            }
        }
        return -1L;
    }

    public static String longToIP(long j) {
        StringBuffer stringBuffer = new StringBuffer("");
        stringBuffer.append(String.valueOf(j >>> 24));
        stringBuffer.append(".");
        stringBuffer.append(String.valueOf((16777215 & j) >>> 16));
        stringBuffer.append(".");
        stringBuffer.append(String.valueOf((WebSocketProtocol.PAYLOAD_SHORT_MAX & j) >>> 8));
        stringBuffer.append(".");
        stringBuffer.append(String.valueOf(j & 255));
        return stringBuffer.toString();
    }

    public static boolean isEncapsulationApp(Context context) {
        if (context != null) {
            String packageName = context.getPackageName();
            PackageManager packageManager = context.getPackageManager();
            if (!TextUtils.isEmpty(packageName) && packageManager != null) {
                try {
                    return isEasyApp(packageManager.getPackageInfo(packageName, 8320).applicationInfo);
                } catch (PackageManager.NameNotFoundException unused) {
                }
            }
        }
        return false;
    }

    public static String encryptInfo(String str, Context context) {
        if (TextUtils.isEmpty(str) || context == null) {
            return "";
        }
        String cryptKey = PublicKeyEntery.getCryptKey(context);
        return !TextUtils.isEmpty(cryptKey) ? encrypt(str, cryptKey) : "";
    }

    public static String encrypt(String str, String str2) {
        try {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes());
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            new Crypto(str2).encrypt(byteArrayInputStream, byteArrayOutputStream);
            return Crypto.byteToHex(byteArrayOutputStream.toByteArray());
        } catch (Exception unused) {
            Log.e(TAG, "Encrpyt string failed!");
            return "";
        }
    }

    public static String decryptInfo(String str, Context context) {
        if (TextUtils.isEmpty(str) || context == null) {
            return "";
        }
        String cryptKey = PublicKeyEntery.getCryptKey(context);
        return !TextUtils.isEmpty(cryptKey) ? decrypt(str, cryptKey) : "";
    }

    public static String decrypt(String str, String str2) {
        try {
            Crypto crypto = new Crypto(str2);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Crypto.hexToByte(str));
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            crypto.decrypt(byteArrayInputStream, byteArrayOutputStream);
            if (byteArrayOutputStream.size() <= 0) {
                Log.e("MDM", "Decrypt string output stream size is 0");
            }
            return byteArrayOutputStream.toString();
        } catch (Exception unused) {
            Log.e(TAG, "Decrpyt string failed!");
            return "";
        }
    }

    private static boolean isEasyApp(ApplicationInfo applicationInfo) {
        return (applicationInfo == null || applicationInfo.metaData == null || TextUtils.isEmpty(applicationInfo.metaData.getString(EASYAPP_META_MARK))) ? false : true;
    }

    /* JADX WARN: Removed duplicated region for block: B:16:0x0035  */
    /* JADX WARN: Removed duplicated region for block: B:17:0x003f  */
    /* JADX WARN: Removed duplicated region for block: B:20:0x004a  */
    /* JADX WARN: Removed duplicated region for block: B:32:0x0050 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public static java.lang.String getHardwareIdByContext(android.content.Context r10) {
        /*
            r0 = 0
            java.lang.String r1 = "VpnCommon"
            if (r10 != 0) goto Lb
            java.lang.String r10 = "getHardwareIdByContext WeakReference get null"
            com.sangfor.bugreport.logger.Log.error(r1, r10)
            return r0
        Lb:
            java.lang.String r2 = "phone"
            java.lang.Object r2 = r10.getSystemService(r2)
            android.telephony.TelephonyManager r2 = (android.telephony.TelephonyManager) r2
            r3 = 0
            r4 = 1
            boolean r5 = checkDeviceIdPermission(r10)     // Catch: java.lang.Exception -> L20
            if (r5 == 0) goto L2e
            java.lang.String r2 = r2.getDeviceId()     // Catch: java.lang.Exception -> L20
            goto L2f
        L20:
            r2 = move-exception
            java.lang.Object[] r5 = new java.lang.Object[r4]
            java.lang.String r2 = r2.getMessage()
            r5[r3] = r2
            java.lang.String r2 = "get deviceId fail"
            com.sangfor.bugreport.logger.Log.error(r1, r2, r5)
        L2e:
            r2 = r0
        L2f:
            boolean r5 = android.text.TextUtils.isEmpty(r2)
            if (r5 == 0) goto L3f
            java.lang.String r2 = "getHardwareId init devid by DeviceIDManger's MacMd5 failed!"
            com.sangfor.bugreport.logger.Log.error(r1, r2)
            java.lang.String r2 = com.sangfor.ssl.common.DeviceIDManger.getAndroidID(r10)
            goto L44
        L3f:
            java.lang.String r10 = "getHardwareId init devid by DeviceIDManger's MacMd5 success!"
            com.sangfor.bugreport.logger.Log.info(r1, r10)
        L44:
            boolean r10 = android.text.TextUtils.isEmpty(r2)
            if (r10 == 0) goto L50
            java.lang.String r10 = "Can't get device's hardware Information."
            com.sangfor.bugreport.logger.Log.error(r1, r10)
            return r0
        L50:
            java.lang.String r10 = "MD5"
            java.security.MessageDigest r10 = java.security.MessageDigest.getInstance(r10)     // Catch: java.security.NoSuchAlgorithmException -> L84
            byte[] r2 = r2.getBytes()     // Catch: java.security.NoSuchAlgorithmException -> L84
            byte[] r10 = r10.digest(r2)     // Catch: java.security.NoSuchAlgorithmException -> L84
            java.lang.StringBuilder r2 = new java.lang.StringBuilder     // Catch: java.security.NoSuchAlgorithmException -> L84
            r5 = 32
            r2.<init>(r5)     // Catch: java.security.NoSuchAlgorithmException -> L84
            int r5 = r10.length     // Catch: java.security.NoSuchAlgorithmException -> L84
            r6 = 0
        L67:
            if (r6 >= r5) goto L7f
            r7 = r10[r6]     // Catch: java.security.NoSuchAlgorithmException -> L84
            java.lang.String r8 = "%02X"
            java.lang.Object[] r9 = new java.lang.Object[r4]     // Catch: java.security.NoSuchAlgorithmException -> L84
            java.lang.Byte r7 = java.lang.Byte.valueOf(r7)     // Catch: java.security.NoSuchAlgorithmException -> L84
            r9[r3] = r7     // Catch: java.security.NoSuchAlgorithmException -> L84
            java.lang.String r7 = java.lang.String.format(r8, r9)     // Catch: java.security.NoSuchAlgorithmException -> L84
            r2.append(r7)     // Catch: java.security.NoSuchAlgorithmException -> L84
            int r6 = r6 + 1
            goto L67
        L7f:
            java.lang.String r10 = r2.toString()     // Catch: java.security.NoSuchAlgorithmException -> L84
            return r10
        L84:
            r10 = move-exception
            java.lang.String r2 = "Can't generate device's hardware ID."
            com.sangfor.bugreport.logger.Log.error(r1, r2, r10)
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.sangfor.ssl.common.VpnCommon.getHardwareIdByContext(android.content.Context):java.lang.String");
    }

    public static String getModelByContext(Context context) {
        return Base64.encodeToString(Build.MODEL.getBytes(), 0);
    }

    public static String getMacAddressByContext(Context context) {
        String wifiMac = DeviceInfo.getWifiMac(context);
        return wifiMac != null ? wifiMac : "00-00-00-00-00-00";
    }

    public static boolean checkDeviceIdPermission(Context context) {
        return Build.VERSION.SDK_INT < 23 || context.checkSelfPermission("android.permission.READ_PHONE_STATE") == 0;
    }
}
